package org.luxor.commons.jdbc.metadata;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author Mr.Yan  @date 2019/9/26$
 */
@ApiModel(description = "分页结果")
public class PageResult<T> implements Serializable {

    @ApiModelProperty(value = "当前页数", example = "1")
    private Long current = 1L;

    @ApiModelProperty(value = "每页记录数", example = "10")
    private Long size = 10L;

    @ApiModelProperty(value = "总记录数", example = "120")
    private Long total = 0L;

    @ApiModelProperty(value = "总页数", example = "12")
    private Long totalPage = 0L;

    @ApiModelProperty(value = "数据结果", example = "[{},{}]")
    private List<T> records = Collections.emptyList();

    /**
     * 分页结果构造函数
     */
    public PageResult(IPage<T> page) {
        if (page == null) {
            return;
        }
        this.total = page.getTotal();
        this.totalPage = page.getPages();
        this.records = page.getRecords();
        this.current = page.getCurrent();
        this.size = page.getSize();
    }

    public Long getCurrent() {
        return current;
    }

    public void setCurrent(Long current) {
        this.current = current;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Long totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "current=" + current +
                ", size=" + size +
                ", total=" + total +
                ", totalPage=" + totalPage +
                ", records=" + records +
                '}';
    }
}
